package com.example.crm.servlet;

import com.example.crm.pojo.Customer;
import com.example.crm.service.ICustomerService;
import com.example.crm.service.impl.CustomerServiceImpl;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/queryUser")
public class QueryCustomerServlet extends HttpServlet {

    ICustomerService customerService;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("queryUser收到请求");

        //1.获取参数
        String name=req.getParameter("cust_name");
        String level=req.getParameter("cust_level");
        Customer customer=new Customer();
        customer.setCustName(name);
        customer.setCustLevel(level);
        //2.调用service
        // 从容器中获取customerServcie的Bean
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        customerService = (ICustomerService)applicationContext.getBean("customerService");
        /*ICustomerService customerService = new CustomerServiceImpl();*/
        List<Customer> list = customerService.findUser(customer);
        req.setAttribute("list",list);
        req.getRequestDispatcher("/WEB-INF/jsp/customer/list.jsp").forward(req,resp);
    }
}
